﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StockLearner
{
    class YahooResponseParser
    {
        public string URI { get; private set; }
        public string Ticker { get; private set; }
        public string Unit { get; private set; }
        public string Timezone { get; private set; }
        public string Currency { get; private set; }
        public string GMTOffset { get; private set; }
        public double PreviousClose { get; private set; }
        public double[] Timestamp { get; private set; }
        public double[] Labels { get; private set; }
        public double[] Values { get; private set; }
        public double[] Close { get; private set; }
        public double[] High { get; private set; }
        public double[] Low { get; private set; }
        public double[] Open { get; private set; }
        public List<YahooDataPoint> Data { get; private set; }

        public YahooResponseParser()
        {
            Data = new List<YahooDataPoint>();
        }

        public void parseDataString(String p_csvInput)
        {
            Data.Clear();
            char [] l_seperators = {'\n'};
            String[] l_csvLines = p_csvInput.Split(l_seperators, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < l_csvLines.Length; i++)
            {
                l_seperators[0] = ':';
                String[] l_lineDelimed = l_csvLines[i].Split(l_seperators, StringSplitOptions.RemoveEmptyEntries);
                if (l_lineDelimed.Length > 1)
                {
                }
                else
                {
                    l_seperators[0] = ',';
                    String[] l_csvSplit = l_lineDelimed[0].Split(l_seperators, StringSplitOptions.RemoveEmptyEntries);
                    YahooDataPoint l_toAdd = new YahooDataPoint();
                    l_toAdd.Timestamp = int.Parse(l_csvSplit[0]);
                    l_toAdd.Close = double.Parse(l_csvSplit[1]);
                    l_toAdd.High = double.Parse(l_csvSplit[2]);
                    l_toAdd.Low = double.Parse(l_csvSplit[3]);
                    l_toAdd.Open = double.Parse(l_csvSplit[4]);
                    l_toAdd.Volume = int.Parse(l_csvSplit[5]);
                    Data.Add(l_toAdd);
                }
            }
        }

        public double[] getTimestamps()
        {
            double[] l_toReturn = new double[Data.Count];

            for (int i = 0; i < Data.Count; i++)
            {
                l_toReturn[i] = (double)Data[i].Timestamp;
            }

            return l_toReturn;
        }

        public double[] getPrices()
        {
            double[] l_toReturn = new double[Data.Count];

            for (int i = 0; i < Data.Count; i++)
            {
                l_toReturn[i] = (Data[i].High + Data[i].Low + Data[i].Open + Data[i].Close) / 4.0;
            }

            return l_toReturn;
        }

        public double[] getVolumes()
        {
            double[] l_toReturn = new double[Data.Count];

            for (int i = 0; i < Data.Count; i++)
            {
                l_toReturn[i] = (double)Data[i].Volume;
            }

            return l_toReturn;
        }
    }
}
